﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using NLog;

namespace SweetSurvey.Controllers
{
    public class ControllerBase: Controller
    {
        #region Fields

        private static Logger logger = LogManager.GetCurrentClassLogger();

        #endregion

        #region Properties

        /// <summary>
        /// The log writer.
        /// </summary>
        protected Logger Logger
        {
            get { return ControllerBase.logger; }
        }

        public new IUrlHelper Url { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// The global error handler for all controllers.
        /// </summary>
        /// <param name="filterContext">The context.</param>
        protected override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);

            // write to log
            Logger.Error<Exception>(filterContext.Exception);
        }

        protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);

            // apply the adaptor for UrlHelper
            this.Url = new UrlHelperAdaptor(requestContext);
        }

        #endregion
    }
}